
#!/bin/bash
function getJsonValueByNode() {
        local json="$1"
        local key="$2"
        # node -pe "JSON.stringify(eval($json).$key)" | awk '{print ($0 == "undefined" ? "" : $0)}'
        node -pe "JSON.stringify(JSON.parse(process.argv[1]).$key)" "$json" | awk '{print ($0 == "undefined" ? "null" : $0)}'
}

function getJsonValueByPython() {
        local key="$1"
        python -c "import json,sys; sys.stdout.write(json.dumps(json.load(sys.stdin).get('$key')));sys.stdout.write('\n');"
}

# example:
# jsond='{"a":1,"b":{"c":"zhouqian"}}'
# getJsonValueByNode "$jsond" "a"
# echo "$jsond" | getJsonValueByPython "a"

doCheck(){
	# rand = date +%s | cut -c1-3
	local randLang=$(openssl rand -base64 8 | cksum | cut -c1-3)
	local randLat=$(openssl rand -base64 8 | cksum | cut -c1-3)
	local time = $(date +%Y%m%d)
	local ampm = $(date +%p)
	local segment == $((ampm == "am" || ampm == "AM" || ampm == "上午"?1:2))
	echo "40.17$randLat,116.24$randLang"
	webjson=$(curl -s "http://api.map.baidu.com/reverse_geocoding/v3/?ak=7KLBuTtkpLskgrzEzNdkkixxvIs5xaHi&output=json&coordtype=bd09&location=40.172$randLat,116.245$randLang")
	address=$(getJsonValueByNode $webjson  "result.formatted_address")
	
	echo "request_data={\"token\":\"fb4aa444-f90e-4cf8-ac97-867fb5d2ade4\",\"work_info\":\"20191030:48:50057766:4:21:1:2\",\"location_type\":$1,\"bssid\":\"\",\"mac_address\":\"\",\"lat\":40.172$randLat,\"lng\":116.245$randLang,\"address\":$address,\"remark\":\"\",\"check_id\":$2}"
	# echo $(curl "http://api.map.baidu.com/geocoder?location=40.172443,116.245651&coord_type=gcj02&output=json")
	# echo "request_data={\"token\":\"fb4aa444-f90e-4cf8-ac97-867fb5d2ade4\",\"work_info\":\"20191030:48:50057766:4:21:1:2\",\"location_type\":$1,\"bssid\":\"\",\"mac_address\":\"\",\"lat\":40.17$randLat,\"lng\":116.24$randLang,\"address\":\"北京市昌平区黄河北街\",\"remark\":\"\",\"check_id\":$2}"
	# curl -i -X POST -k -H "'Content-type':'application/json'" -d "request_data={\"token\":\"fb4aa444-f90e-4cf8-ac97-867fb5d2ade4\",\"work_info\":\"20191030:48:50057766:4:21:1:2\",\"location_type\":$1,\"bssid\":\"\",\"mac_address\":\"\",\"lat\":40.172443,\"lng\":116.245651,\"address\":\"北京市昌平区黄河北街\",\"remark\":\"\",\"check_id\":$2}" https://app.cnpc.com.cn//core/attendance/check/doCheck
	curl -i -X POST -k -H "'Content-type':'application/json'" -d "request_data={\"token\":\"fb4aa444-f90e-4cf8-ac97-867fb5d2ade4\",\"work_info\":\"$time:48:50057766:4:21:1:2\",\"location_type\":$1,\"bssid\":\"\",\"mac_address\":\"\",\"lat\":40.172$randLat,\"lng\":116.245$randLang,\"address\":$address,\"remark\":\"\",\"check_id\":$2}" https://app.cnpc.com.cn//core/attendance/check/doCheck
}

getUserInfo(){
	curl -i -X POST -k -H "'Content-type':'application/json'" -d "request_data={\"token\":\"fb4aa444-f90e-4cf8-ac97-867fb5d2ade4\"}" https://app.cnpc.com.cn//core/attendance/check/getCheckInfo
}

personalMonthly(){
	curl -i -X POST -k -H "'Content-type':'application/json'" -d "request_data={\"token\":\"fb4aa444-f90e-4cf8-ac97-867fb5d2ade4\",\"date_time\":$1}" https://app.cnpc.com.cn//core/attendance/statistics/personalMonthly
}

if [[ $1 == "docheck" ]]; then
	doCheck $2 $3
fi

if [[ $1 == "getuserinfo" ]]; then
	getUserInfo
fi

if [[ $1 == "statistic" ]]; then
	personalMonthly $2
fi

# http://api.map.baidu.com/geoconv/v1/?coords=116.245651,40.172443&from=1&to=5&ak=7KLBuTtkpLskgrzEzNdkkixxvIs5xaHi
# http://api.map.baidu.com/reverse_geocoding/v3/?ak=7KLBuTtkpLskgrzEzNdkkixxvIs5xaHi&output=json&coordtype=wgs84ll&location=340.172974,116.241843

# http://api.map.baidu.com/reverse_geocoding/v3/?ak=7KLBuTtkpLskgrzEzNdkkixxvIs5xaHi&output=json&coordtype=bd09&location=40.172974,116.241843

http://e.cnpc.com.cn/attendance/prod/attendanceapp/?randomNumber=ivRgxk&token=fb8067cf-44aa-4719-a866-9327402c5612&rx_token=fb8067cf-44aa-4719-a866-9327402c5612
